package middlewares

import (
	"gozeroSingle/utils"
	"net/http"

	sentinel "github.com/alibaba/sentinel-golang/api"
	"github.com/zeromicro/go-zero/rest/httpx"
)

/**
 * 并发
 *
 * @param http.HandlerFunc next ------------------------------必传
 * @return http.HandlerFunc
 * @author huwl
 */
func IsolationMiddleware(next http.HandlerFunc) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		e, b := sentinel.Entry("request-isolation", sentinel.WithBatchCount(1))
		if b != nil {
			httpx.OkJsonCtx(r.Context(), w, utils.ErrorOutput("并发量太大"))
			return
		}

		e.Exit()

		next(w, r)
	}
}
